<?php
App::import('Vendor', 'WebServicesController');

class ServersController extends WebServicesController
{
    var $name           = 'Servers';
    var $uses           = array('Person');
    var $autoRender     = false;    

    // all method exposed by our soap server
    
    var $api=array(                                   
                   'simpletest' => array('doc'   => 'Simple test, gets an integer from input, return integer * 2',
                                         'input' => array('appid' => 'xsd:integer'),
                                         'output' => array('return' => 'xsd:integer')),               
                   'sumtest' => array('doc'   => 'Sum test, gets 2 integer return the sum',
                                         'input' => array('int1' => 'xsd:integer', 'int2' => 'xsd:integer'),
                                         'output' => array('return' => 'xsd:integer')), 
                   'sumall' => array('doc'   => 'gets all values passed and return the sum',
                                         'input' => array('int1' => 'xsd:integer'),
                                         'output' => array('return' => 'xsd:integer')),   
                   'doubleall' => array('doc'   => 'all values passed are returned as double',
                                         'input' => array('int1' => 'xsd:integer'),
                                         'output' => array('return' => 'intarray')),                                           
                   'sumteststruct' => array('doc'   => 'Sum test, gets 2 integer in a complex type, return the sum',
                                            'input' => array('param' => 'twoint'),
                                            'output' => array('return' => 'xsd:integer')),
                   'sumteststructsingle' => array('doc'   => 'Sum test, gets 2 integer in a complex type and another one, return the sum',
                                            'input' => array('param' => 'twoint',
                                                             'another' => 'xsd:integer'),
                                            'output' => array('return' => 'xsd:integer')), 
                   'sumtestlevelstruct' => array('doc'   => 'Sum test, gets 2 integer in a complex type and another one, return the sum',
                                                 'input' => array('param' => 'twoint',
                                                                  'thers' => 'xsd:integer'),
                                                 'output' => array('return' => 'twoint')),                                             
                   'getpersonbyid' => array('doc'   => 'Retrive Person record by ID',
                                                 'input' => array('personid' => 'xsd:integer'),
                                                 'output' => array('return' => 'Person')),
                   'getpersonbyidnotimestamp' => array('doc'   => 'Retrive Person record by ID, cleaned created/modified timestamp',
                                                       'input' => array('personid' => 'xsd:integer'),
                                                       'output' => array('return' => 'cleanfullperson')),
                   'getpersonbyidfirstname' => array('doc'   => 'Retrieve Person record by ID, only first name',
                                                       'input' => array('personid' => 'xsd:integer'),
                                                       'output' => array('return' => 'firstnameperson')),
                   'getpersonbyidfirstnameservertime' => array('doc'   => 'Retrieve Person record by ID',
                                                       'input' => array('personid' => 'xsd:integer'),
                                                       'output' => array('return' => 'firstnamepersonservertime')),
                    'getallpersons' => array('doc'   => 'Retrieve All Person records',
                                                       'output' => array('return' => 'allpersons')), 
                    'getallpersonsfirstname' => array('doc'   => 'Retrieve All Person records (firstname only)',
                                                       'output' => array('return' => 'allpersonsfirstname')), 
                  );
    
    // complex type definition
    
    var $complexTypes = array('twoint' => array('struct' => array('int1' => 'xsd:integer',
                                                                  'int2' => 'xsd:integer')),
                              'intarray'   => array('array' => 'xsd:integer'),  
                              'fullperson' => array('model' => 'Person'),                                    
                              'cleanfullperson' => array('model' => 'Person', 'ignoreFields' => array('created', 'modified')),                                    

                              'firstnameperson' => array('model' => 'Person', 'onlyFields' => array('firstname')),                                    

                              'firstnamepersonservertime' => array('struct' => array(array('model' => 'Person', 'onlyFields' => array('firstname')),
                                                                                           'servertime' => 'xsd:string')),
                              'allpersons'                => array('array' => 'fullperson'),
                              'allpersonsfirstname'       => array('array' => 'firstnameperson'),                             
                             );
    
    
    // simpletest, just get an integer and return it's double
    
    function simpletest($inp)
    {
        return $inp * 2;
    }
    
    // get 2 integers and return the sum
    
    function sumtest()
    {
        $v = func_get_args();
        if(count($v) != 2)
            $this->_soap_server->fault(20, '', 'Wrong number of parameters passed, Expected 2');
        else
            return array_sum($v);
    }

    // get N integers and return the sum (actually similar to above, but no checking for errors)
    
    function sumall()
    {
        $v = func_get_args();
        if(!empty($v))
            return array_sum($v);
        return 0;
    }

    // get N integers and return all values * 2
    
    function doubleall()
    {
        $v = func_get_args();
        if(!empty($v))
        {
            array_walk($v,create_function('&$elem','$elem *= 2;'));
            return $v;
        }
        $this->_soap_server->fault(20, '', 'Wrong number of parameters passed');
        
        return 0;
    }

    // sum test with input as structure

    function sumteststruct()
    {
        $v = func_get_args();
        if(count($v) < 1 || !is_array($v[0]) || count($v[0]) != 2)
            $this->_soap_server->fault(20, '', 'Wrong number of parameters passed, Expected array of 2  values');
        else
            return array_sum($v[0]);
    } 

    // sum test with input as structure

    function sumteststructsingle()
    {
        $v = func_get_args();
        if(count($v) < 2 || !is_array($v[0]) || count($v[0]) != 2)
            $this->_soap_server->fault(20, '', 'Wrong number of parameters passed, Expected array of 2  values');
        else
            return array_sum($v[0]) + $v[1];
    }     

    // sum test by array level
    
    function sumtestlevelstruct()
    {
        $v = func_get_args();
        if(count($v) < 2 || !is_array($v[0]) || count($v[0]) != 2)
            $this->_soap_server->fault(20, '', 'Wrong number of parameters passed, Expected array of 2  values');
        else
            return array('int1' => array_sum($v[0]), 'int2' => array_sum($v));
    }

    // get the person by ID
    function getpersonbyid($id)
    {
        $v = $this->Person->findById($id);
        
        if(empty($v))
            $this->_soap_server->fault(1, '', 'This person does not exist');
        else    
            return $v['Person'];
    }     

    // get the person by ID

    function getpersonbyidnotimestamp($id)
    {
        // be sure to turn the debug < 2, the cakeDebug will screw up the xml
        
        if(Configure::read() >= 2)
            Configure::write(array('debug' => 1));
        $v = $this->Person->findById($id);
        
        if(empty($v))
            $this->_soap_server->fault(1, '', 'This person does not exist');
        else    
            return $v['Person'];
    }     

    // get the person by ID only firstname
    
    function getpersonbyidfirstname($id)
    {
        $v = $this->Person->findById($id);
        
        if(empty($v))
            $this->_soap_server->fault(1, '', 'This person does not exist');
        else    
            return $v['Person'];
    }

    // get the person by ID only firstname + server date/time
    
    function getpersonbyidfirstnameservertime($id)
    {
        $v = $this->Person->findById($id);
        
        if(empty($v))
            $this->_soap_server->fault(1, '', 'This person does not exist');
        else
        {
            $v['Person']['servertime'] = date(DATE_ATOM);
            return $v['Person'];
        }
    }

    // get the person by ID only firstname
    
    function getallpersons()
    {
        $v = $this->Person->find('all');
        if(empty($v))
            $this->_soap_server->fault(1, '', 'Person table is empty');
        else    
            return set::extract($v, '{n}.Person');
    }
    
    // get the person by ID only firstname
    
    function getallpersonsfirstname()
    {
        // be sure to turn the debug < 2, the cakeDebug will screw up the xml
        
        if(Configure::read() >= 2)
            Configure::write(array('debug' => 1));
        $v = $this->Person->find('all');
        if(empty($v))
            $this->_soap_server->fault(1, '', 'Person table is empty');
        else    
            return set::extract($v, '{n}.Person');
    }    
}
?>